/**
 * Created by Administrator on 2017/5/24.
 */
import lorck from 'lockr';
export default  async (uri, data, method) => {
  try {
    if (window.fetch) {
      method = method.toLocaleLowerCase();
      let prefix_data = '', config = {}, myHeader = '', authKey = '', sessionId = '';
      myHeader = new Headers();
      authKey   = lorck.get('key');
      sessionId = lorck.get('s_id');
      myHeader.append('authKey', authKey);
      myHeader.append('sessionId', sessionId);
      config.method = method.toLocaleUpperCase();
      config.credentials = 'include';
      config.headers = myHeader;
      config.mode = 'cors';
      if (method === 'get') {
        let keys = Object.keys(data), value = Object.values(data);
        let delimiter = '&', prefix = '?';
        for (let key in keys) {
          prefix_data += delimiter + keys[key] + '=' + value[key];
        }
        prefix_data = prefix_data.substr(1);
        uri = uri + prefix + prefix_data;
        config.method = method;
      }
      if (method === 'post') {
        prefix_data = JSON.stringify(data);
        config.body = prefix_data;
      }
      return await fetch(uri, config);
    }
  } catch (e) {
    console.debug('error message:' + e);
  }
}
